//
//  CTResetPasswordViewController.m
//  CT
//
//  Created by liufanwei on 15/4/8.
//  Copyright (c) 2015年 liufanwei. All rights reserved.
//

#import "CTResetPasswordViewController.h"
#import "CTNetHelper.h"
#import "CTUserDefaultHelper.h"
#import "CommonMicro.h"
#import "UILabel+WBExtension.h"
#import "BDKNotifyHUD.h"


@interface CTResetPasswordViewController ()<UITextFieldDelegate>{
    BOOL isGetCode;
    NSString * _Account;
}
@property (weak, nonatomic) IBOutlet UITextField *TextField;
@property (weak, nonatomic) IBOutlet UIButton *Button;

@end

@implementation CTResetPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.barTintColor = kBackGroundColor;
    UILabel * title = [UILabel labelWithText: @"重置密码" font:[UIFont fontWithName:kFontName size:20]];
    title.textColor = [UIColor whiteColor];
    self.navigationItem.titleView = title;
    
    _TextField.clipsToBounds = YES;
    _TextField.layer.borderColor = kBackGroundColor.CGColor;
    _TextField.delegate = self;
    _TextField.font = [UIFont fontWithName:kFontName size:21];
    _TextField.layer.borderWidth = 1;
    _TextField.keyboardType = UIKeyboardTypeNumberPad;
    
    [_Button setImage:[UIImage imageNamed:@"verificationcodeb"] forState:UIControlStateNormal];
    [_Button setImage:[UIImage imageNamed:@"verificationcodey"] forState:UIControlStateHighlighted];
    isGetCode = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (IBAction)buttonClick:(id)sender {
    NSString * account = _TextField.text;
    if (isGetCode) {
        if (account == Nil || account.length == 0) {
            [BDKNotifyHUD notifyHUDWithText:@"输入你的账号"];
            return;
        }
        if(account.length != 11 || account.intValue == 0){
            [BDKNotifyHUD notifyHUDWithText:@"非法账号！账号为你注册手机号"];
            return;
        }
        [[CTNetHelper ShareInstance] passwordVerify:@{@"number":account} WithCallBack:^(id data) {
            [_TextField resignFirstResponder];
            isGetCode = NO;
            NSInteger code = [[data objectForKey:@"code"] integerValue];
            if (code == 0) {
                _Account = account;
                _TextField.keyboardType = UIKeyboardTypeDefault;
                _TextField.placeholder = @"输入短信验证码";
                _TextField.text = Nil;
                [_Button setImage:[UIImage imageNamed:@"resetpasswordb"] forState:UIControlStateNormal];
                [_Button setImage:[UIImage imageNamed:@"resetpasswordy"] forState:UIControlStateHighlighted];
            }else{
                NSString * msg = [data objectForKey:@"msg"];
                [BDKNotifyHUD notifyHUDWithText:msg];
            }
        }];
    }else{
        if(!_Account){
            return;
        }
        if (account == Nil || account.length == 0) {
            [BDKNotifyHUD notifyHUDWithText:@"输入你的验证码"];
            return;
        }
        [[CTNetHelper ShareInstance] resetPassword:@{@"pin":_Account,@"code":account} WithCallBack:^(id data) {
            NSInteger code = [[data objectForKey:@"code"] integerValue];
            NSString * msg = [data objectForKey:@"msg"];
            if (code == 0) {
                msg = @"重置成功！请返回登录界面登录";
            }
            [BDKNotifyHUD notifyHUDWithText:msg];
        }];
    }
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (isGetCode && textField.text.length > 10 && string.length != 0 && string.length != 0) {
        return NO;
    }
    return YES;
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
